package homeword.traffic_light;

import java.util.concurrent.locks.ReentrantLock;

public class Repository {
    ReentrantLock lock = new ReentrantLock(true);
    /**
     * 公共的仓库
     */

    private int total;
    /**
     * 红灯和绿灯共同闪烁5下
     * 黄灯闪烁3下
     */
    private int RED_AND_GREEN = 5;
    private int YELLOW = 3;

    public synchronized void red() throws InterruptedException {
        String name = Thread.currentThread().getName();

        this.total = 0;
        while (total < RED_AND_GREEN) {
            this.total++;
            System.out.println(name + "--第" + this.total + "下");
        }
    }

    public synchronized void yellow() throws InterruptedException {

        String name = Thread.currentThread().getName();
        this.total = 0;
        while (total < YELLOW){
            this.total++;
            System.out.println(name + "--第" + this.total + "下");
        }
    }

    public synchronized void green() throws InterruptedException {
        String name = Thread.currentThread().getName();

        this.total = 0;
        while (total < RED_AND_GREEN) {
            this.total++;
            System.out.println(name + "--第" + this.total + "下");
        }
    }
}
